import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from src.day36.pages.base_page import BasePage


class AddDepartment(BasePage):
    __DEPARTMENT_NAME = (By.CSS_SELECTOR, '[name="name"]')
    __DEPARTMENT_LIST_BTN = (By.CSS_SELECTOR, ".qui_btn.ww_btn.ww_btn_Dropdown.js_toggle_party_list")
    __ROOT_DEPARTMENT = (By.CSS_SELECTOR, ".js_party_list_container>div>ul>li:nth-child(1)>a")
    __SAVA_BTN = (By.CSS_SELECTOR, ".qui_btn.ww_btn.ww_btn_Blue")
    __TIPS = (By.ID, 'js_tips')

    def add_department(self,name):
        """
        添加部门信息，并保存
        :return:
        """
        from src.day36.pages.contact_page import ContactPage  #局部导入,为了防止循环导入出错
        with allure.step("填写部门信息并保存"):
            self.find_and_send(*self.__DEPARTMENT_NAME, text=name)
            self.find_and_click(*self.__DEPARTMENT_LIST_BTN)
            self.find_and_click(*self.__ROOT_DEPARTMENT)
            self.find_and_click(*self.__SAVA_BTN)
        WebDriverWait(self.driver,5).until(expected_conditions.visibility_of_element_located((By.ID, 'js_tips')))
        return ContactPage(self.driver)
